# 1  验证用户名和密码：

import re
from lib2to3.pygram import pattern_grammar
from conda.notices.core import retrieve_notices


def login(username):
    patten = r'^[a-zA-Z]\w{5,15}$'
    if re.fullmatch(patten,username):
        return True
    else:
        return False
user_name = ['jack6666','tim9999','00tim','tim','jack']
for user_name in user_name:
    is_valid = login(user_name)
    print(f"{user_name}:{'合法'if is_valid else '不合法'}")

# 验证电话号码

def phone_num(number):
    pattern = r'^(\d{3,4}-)\d{7,8}$'
    if re.fullmatch(pattern,number):
        return True
    else:
        return False
test_num = {
    '122-212321332321','12223-213223123','222-1222222','010-122345','112-12345566'
}
for num in test_num:
    is_valid = phone_num(num)
    print(f"'{num}':{'合理'} if is_valid else  '不合理'")

# 验证手机号
def phone_num(number):
    pattern = r'^1([38][0-9]|4[5-9]|5[0-3,5-9]|66|7[0-8]|9[89])[0-9]{8}$'
    if re.fullmatch(pattern,number):
        return True
    else:
        return False
test_num = ['13812345678','18312345678','12212345678','13212345678','18212345678']

# 验证邮箱地址
def mail(email):
    pattern = r'^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$'
    if re.fullmatch(pattern, email):
        return True
    else:
        return False
test_mail =[
    'jack@qq.com','tim1234','1222@163.com'
]
for email in test_mail:
    is_valid = mail(email)
    print(f"'{email}':{'合法' if is_valid else '不合法'}")

#  数字字母组成的字符串
def new_str(text):
    pattern = r'^[A-Za-z0-9]+$'
    if re.fullmatch(pattern, text):
        return True
    else:
        return False
test_str = [
    ' hello ','Abc123','Abc 134','1234c','12321Dd'
]
for text in test_str:
    is_valid = new_str(text)
    print(f"'{text}': {' 合法' if is_valid else ' 不合法'}")

# 整数或者小数
def new_num(nums):
    pattern = r'^[0-9]+([.][0-9]+)?$'
    if re.fullmatch(pattern, nums):
        return True
    else:
        return False
test_num = ['123','1.qq','1.11','1 21','12e2','12 s']
for nums in test_num:
    is_valid = new_num(nums)
    print(f"'{nums}': {' 合法' if is_valid else ' 不合法'}")

#  只能输入多位数字
def num_nums(numbers,n):
    pattern = fr'^\d{n}$'
    if re.fullmatch(pattern,numbers):
        return True
    else:
        return False
test_numbers=[('1234',4),('0011',4),('21aa',4),('111',3)]
for numbers,n  in test_numbers:
    is_valid = num_nums(numbers,n)
    print(f"'{numbers}' (n = {n}): {' 合法' if is_valid else ' 不合法'}")

# 只能输入m,n位数字

def m_n_num(num_str,m,n):
    pattern = fr'^\d{m, n}$'
    if re.fullmatch(pattern, num_str):
        return True
    else:
        return False
test_num = [('123',3,5),('0011',3,5),('21 a',3,5),('211 ',3,5)]
for num_str,m,n in test_num:
    is_valid = m_n_num(num_str,m,n)
    print(f"'{num_str}' (m={m} ,n = {n}): {' 合法' if is_valid else ' 不合法'}")

# 输入有两位小数的正实数
def positive_num(pos_nums):
    pattern = r'^[0-9]+(\.[0-9]{2})?$'
    if re.fullmatch(pattern, pos_nums):
        return True
    else:
        return False
test_num = ['123','1234','122.22','123.''122.1']
for pos_nums in test_num:
    is_valid = positive_num(pos_nums)
    print(f"'{pos_nums}': {' 合法' if is_valid else ' 不合法'}")

#  只能输入汉字
def chinese(text):
    pattern = r'^[\u4e00-\u9fa5]+$'
    if re.fullmatch(pattern, text):
        return True
    else:
        return False
test_str = ['中国','你好','jack','tim']
for text in test_str:
    is_valid = chinese(text)
    print(f"'{text}': {' 合法' if is_valid else ' 不合法'}")

# 验证URl
def url(url_text):
    pattern = r'^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$'
    if re.fullmatch(pattern, url_text):
        return True
    else:
        return False
text_url =['https://baike.baidu.com/','http://123.abc','jack.com','mike.com']
for url_text in text_url:
    is_valid=url(url_text)
    print(f"'{url_text}': {' 合法' if is_valid else ' 不合法'}")

# 获取日期
def date(date_str):
   pattern = r'(\d{4})年(0[1-9]|1[0-2])月(0[1-9]|[12][0-9]|3[01])日?$'
   return bool(re.fullmatch(pattern,date_str,re.VERBOSE))
test_date = [
    '2023-22-12','2020年02月01日','2025年8月13日','2001-22-1212'
]
for date_str in test_date:
    print(f"'{date}': {' 合法' if is_valid else ' 不合法'}")

